<?php
/**
 * 日志,页面管理
 *
 * $Id: class.blog.php 238 2011-01-13 08:53:47Z bwanglei@gmail.com $
 */

class awBlog {
	/**
	 * 内部数据对象
	 * @var MySQL
	 */
	private $db;

	function __construct() {
		$this->db = MySQL::getInstance();
	}

	/**
	 * 添加日志 页面
	 *
	 * @param array $logData
	 * @return int
	 */
	function addlog($logData) {
		$kItem = array();
		$dItem = array();
		foreach ($logData as $key => $data) {
			$kItem[] = $key;
			if (in_array($key,array('title','content'))) $data = addslashes($data);	//入库转义
			$dItem[] = $data;
		}
		$field = implode(',', $kItem);
		$values = '\''.implode('\',\'', $dItem).'\'';
		$this->db->query('INSERT INTO `'.DB_PREFIX.'articles` ('.$field.') VALUES ('.$values.')');
		$logid = $this->db->insert_id();
		return $logid;
	}

	/**
	 * 更新日志内容
	 * @param array $logData
	 * $param int $blogId
	 */
	function updateLog($logData, $blogId) {
		$Item = array();
		foreach ($logData as $key => $data) {
			if (in_array($key,array('title','content'))) $data = addslashes($data);	//入库转义
			$Item[] = ('`'.$key.'`'.'=\''.$data.'\'');
		}
		$upStr = implode(',', $Item);
		$this->db->query('UPDATE `'.DB_PREFIX.'articles` SET '.$upStr.' WHERE id='.$blogId);
	}

	/**
	 * 获得指定条件的日志条数
	 *
	 * @param string $confition
	 * @param string $type
	 * @return int
	 */
	function getLogNum($condition='', $type='article') {
		if ($spot == 0) {
			$author = '';
		} else {
			$author = ROLE == 'admin' ? '' : ' and author='.UID;
		}

		$res = $this->db->query('SELECT id FROM `'.DB_PREFIX.'articles` WHERE type=\''.$type.'\''.$author.$condition);
		$logNum = $this->db->num_rows($res);
		return $logNum;
	}

	/**
	 * 后台获取单条日志
	 *
	 * @param int $blogId
	 * @return array
	 */
	function getOneLogForAdmin($blogId) {
		global $timezone;
		$sql = 'SELECT * FROM `'.DB_PREFIX.'articles` WHERE id='.$blogId;
		$res = $this->db->query($sql);
		$row = $this->db->fetch_array($res);
		if ($row) {
			$row['time_date'] = $row['date'] + $timezone*3600;
			$row['title'] = htmlspecialchars(stripslashes($row['title']));
			$row['content'] = stripslashes($row['content']);
			$logData = $row;
			return $logData;
		}
		return false;
	}

	/**
	 * 前台获取单条日志
	 *
	 * @param int $blogId
	 * @return array
	 */
	function getOneLogForHome($blogId) {
		global $timezone;
		$sql = 'SELECT * FROM `'.DB_PREFIX.'articles` WHERE id='.$blogId;
		$res = $this->db->query($sql);
		$row = $this->db->fetch_array($res);
		if (!$row) return false;
		$row['title'] = htmlspecialchars(stripslashes($row['title']));
		$row['content'] = stripslashes($row['content']);
		$row['time_data'] = $row['time_data'] + $timezone * 3600;
		$logData = $row;
		return $logData;
	}

	/**
	 * 获取日志列表
	 *
	 * @param string $condition
	 * @param int $page
	 * @param int $prePageNum
	 * @return array
	 */
	function getLogsForHome($condition='', $bookmark, $prePageNum) {
		global $timezone;
		//设定查询字符串
		if ($bookmark == '') {
			$sql_condition = ($condition=='') ? $condition : ' WHERE '.$condition;
			$sql = 'SELECT * FROM `'.DB_PREFIX.'articles`'.$sql_condition.' ORDER BY `time_date` DESC LIMIT '.($prePageNum+1);
			$pre_sql = '';
		} else {
			$sql_condition = ' WHERE `time_date`<='.$bookmark.(($condition=='') ? '' : ' AND'.$condition);
			$pre_condition = ' WHERE `time_date`>'.$bookmark.(($condition=='') ? '' : ' AND'.$condition);
			$sql = 'SELECT * FROM `'.DB_PREFIX.'articles`'.$sql_condition.' ORDER BY `time_date` DESC LIMIT '.($prePageNum+1);
			$pre_sql = 'SELECT * FROM `'.DB_PREFIX.'articles`'.$pre_condition.' ORDER BY `time_date` ASC LIMIT '.$prePageNum;
		}

		$result_articles = $this->db->query($sql);
		$count_result = $this->db->num_rows($result_articles);
		if ($count_result <= $prePageNum) {
			$next_bookmark = false;
		} else {
			mysql_data_seek($result_articles,$count_result-1);	//结果集的指针到最后一行
			$next_bookmark = $this->db->fetch_array($result_articles);	//取出最后一行的结果
			$next_bookmark = $next_bookmark['time_date'];
			mysql_data_seek($result_articles,0);	//指针复原
		}
		//取得上一页的bookmark
		if ($pre_sql != '') {
			$pre_articles = $this->db->query($pre_sql);
			$count_pre = $this->db->num_rows($pre_articles);
			if ($count_pre == 0) {
				$pre_bookmark = false;
			} else {
				mysql_data_seek($pre_articles, ($count_pre-1));
				$pre_bookmark = $this->db->fetch_array($pre_articles);
				$pre_bookmark = $pre_bookmark['time_date'];
			}
		} else {
			$pre_bookmark = false;
		}
		$logs = array();
		//取得结果集
		while ($row = $this->db->fetch_array($result_articles)) {
			$row['time_date'] += $timezone*3600;
			$row['title'] = htmlspecialchars(trim(stripslashes($row['title'])));
			$row['content'] = stripslashes($row['content']);
			$logs[$row['id']] = $row;
		}
		if (count($logs) > $prePageNum) array_pop($logs);
		return array($logs,$pre_bookmark,$next_bookmark);
	}

	/**
	 * 删除日志
	 *
	 * @param int $blogId
	 */
	function deleteLog($blogId) {
		$this->db->query('DELETE FROM `'.DB_PREFIX.'articles` WHERE `id`='.$blogId);
		//评论
		$this->db->query('DELETE FROM `'.DB_PREFIX.'comments` WHERE id='.$blogId);
		//标签
		$this->db->query('DELETE FROM `'.DB_PREFIX.'tags` WHERE `aid`='.$blogId);
	}

	/**
	 * 增加阅读次数
	 *
	 * @param int $blogId
	 */
	function updateViewCount($blogId) {
		$this->db->query('UPDATE `'.DB_PREFIX.'articles` SET views=views+1 WHERE id='.$blogId);
	}

	/**
	 * 获取相邻日志
	 *
	 * @param int $date unix时间戳
	 * @return array
	 */
	function neighborLog($date) {
		$neighborlog = array();
		$neighborlog['nextLog'] = $this->db->once_fetch_array('SELECT title,id FROM `'.DB_PREFIX.'articles` WHERE time_date<'.$date.' and type=\'article\' ORDER BY time_date DESC LIMIT 1');
		$neighborlog['prevLog'] = $this->db->once_fetch_array('SELECT title,id FROM `'.DB_PREFIX.'articles` WHERE time_date>'.$date.' and type=\'article\' ORDER BY time_date LIMIT 1');
		if ($neighborlog['nextLog']) {
			$neighborlog['nextLog']['title'] = htmlspecialchars(stripslashes($neighborlog['nextLog']['title']));
		}
		if ($neighborlog['prevLog']) {
			$neighborlog['prevLog']['title'] = htmlspecialchars(stripslashes($neighborlog['prevLog']['title']));
		}
		return $neighborlog;
	}
}
